package com.wl.zlmanage.controller;

import com.alibaba.fastjson.JSONObject;
import com.wl.zlmanage.bean.User;
import com.wl.zlmanage.bean.ZlInfo;
import com.wl.zlmanage.mapper.ResourceMapper;
import com.wl.zlmanage.mapper.UserMapper;
import com.wl.zlmanage.utils.Des;
import com.wl.zlmanage.utils.TimeUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by wanglei on 2018/5/25.
 */
@Controller
public class LoginController {

    public static final String ENC_KEY = "encKey";

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    private static Des des = new Des();

    @Autowired
    UserMapper userDao;

    @RequestMapping(value = {"/login.do"}, method = {
        RequestMethod.POST}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String login(HttpServletRequest request, HttpSession session) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String encKey = (String) session.getAttribute(ENC_KEY);
        String password = "";
        String userId = "";
        try {
            password = des.strDec(request.getParameter("password"), encKey, encKey, encKey);
            userId = des.strDec(request.getParameter("userId"), encKey, encKey, encKey);
        } catch (Exception e) {
            resultMap.put("result", "登录失败，用户名密码解密失败");
            logger.info("登录失败,用户名密码解密失败");
            return JSONObject.toJSONString(resultMap);
        }
        session.removeAttribute(ENC_KEY);

        //根据用户名查询用户信息
        User user = userDao.getUserById(userId);
        //账号不存在
        if (user == null || !user.getUserId().equals(userId)) {
            resultMap.put("result", "用户名密码错误!");
            logger.info("登录失败,用户名错误，用户名:" + userId);
            return JSONObject.toJSONString(resultMap);
        }

        if (!user.getPassword().equals(password)) {
            resultMap.put("result", "用户名密码错误!");
            logger.info("登录失败,密码错误，密码:" + password);
            return JSONObject.toJSONString(resultMap);
        } else {
            session = request.getSession();
            session.setAttribute("username", user.getUserId());
            session.setAttribute("userId", user.getId());
            //session.setMaxInactiveInterval(10);
            logger.info(userId + "登录了系统");
            resultMap.put("result", "index");
            return JSONObject.toJSONString(resultMap);
        }
    }

    @RequestMapping(value = "/getEncKey.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getEncKey(HttpServletRequest request, HttpServletResponse response,
        HttpSession session) {
        String encKey = String.valueOf(System.currentTimeMillis());
        session.setAttribute(ENC_KEY, encKey);
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("encKey", encKey);
        return JSONObject.toJSONString(resultMap);
    }

    /**
     * @return {@link }
     * @Title logout
     * @Description 退出
     * @since 2017年7月11日 下午6:02:19
     */
    @RequestMapping(value = "/loginout.do")
    @ResponseBody
    public String logout(HttpSession session) {
        session.removeAttribute("username");
        session.removeAttribute("userId");
        return "success";
    }


    @RequestMapping(value = "/getLoginInfo.do")
    @ResponseBody
    public User getLoginInfo(HttpSession session) {
        String userId = (String) session.getAttribute("username");
        User user = userDao.getUserById(userId);
        return user;
    }


}
